package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketBrand;
import com.cskaoyan.market.service.MarketBrandService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @Description: 品牌制造商的所有操作
 * @Author: 鲸落
 * @Date: 2024/05/06/14:59
 * @Version: 1.0
 */
@RestController
@RequestMapping("admin/brand")
public class AdminBrandController{
    @Autowired
    private MarketBrandService marketBrandService;

    @PostMapping("delete")
    public Object delete(@RequestBody MarketBrand marketBrand) {
        Integer id = marketBrand.getId();
        Integer row = marketBrandService.deleteBrandMerchantsById(id);
        if (row == 1) {
            return ResponseUtil.ok();
        } else {
            return ResponseUtil.fail(-1, "删除失败");
        }
    }

    @PostMapping("update")
    public Object update(@RequestBody MarketBrand marketBrandRequest) {
        // 请求参数位于请求体中，是一个Json字符串
        // 参数非法判断
        String desc = marketBrandRequest.getDesc();
        BigDecimal floorPrice = marketBrandRequest.getFloorPrice();
        if (StringUtils.isEmpty(desc) || (floorPrice.compareTo(BigDecimal.ZERO) == 0)) {
            return ResponseUtil.badArgumentValue();
        }
        // 调用service处理具体的业务逻辑
        MarketBrand marketBrand = marketBrandService.updateBrandMerchants(marketBrandRequest);
        return ResponseUtil.ok(marketBrand);
    }

    @PostMapping("create")
    public Object create(@RequestBody Map map){
        // desc、floorPrice的校验：不能为空  及floorPrice的二次检验：必须是数字
        String name = (String) map.get("name");
        String desc = (String) map.get("desc");
        String floorPrice = (String) map.get("floorPrice");
        String picUrl = (String) map.get("picUrl");
        BigDecimal floorPriceBigDecimal = null;
        try {
            if (StringUtils.isEmpty(desc) || StringUtils.isEmpty(floorPrice)) {
                return ResponseUtil.badArgumentValue();
            }
            floorPriceBigDecimal = new BigDecimal(floorPrice);
        } catch (NumberFormatException e) {
            return ResponseUtil.badArgumentValue();
        }
        // 调用service处理具体的业务逻辑
        MarketBrand marketBrand = marketBrandService.createBrandMerchants(name, desc, floorPrice, picUrl);
        return ResponseUtil.ok(marketBrand);
    }

    @GetMapping("list")
    public Object list(String page,String limit,String idParam,String name,String sort,String order){
        Integer pageParam = null;
        Integer limitParam = null;
        Integer id = null;
        try {
            pageParam = Integer.parseInt(page);
            limitParam = Integer.parseInt(limit);
            if (!StringUtils.isEmpty(idParam)) {
                id = Integer.parseInt(idParam); // 输入的参数id不为null时才进行判断是否合法
            }
        } catch (NumberFormatException e) {
            return ResponseUtil.badArgumentValue();
        }
        // 调用service处理具体的业务逻辑
        List<MarketBrand> marketBrandList = marketBrandService.list(pageParam, limitParam, idParam, name, sort, order);
        // 返回响应
        return ResponseUtil.okList(marketBrandList);
    }
}